package com.csun.cmny.provider.controller.rpc;

import com.csun.cmny.provider.model.dto.user.OmcUserQueryDto;
import com.csun.cmny.provider.model.dto.user.UserRoleDto;
import com.csun.cmny.provider.model.service.UacUserFeignApi;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.model.vo.OpcUserVo;
import com.csun.cmny.provider.model.vo.RpcMerchantVO;
import com.csun.cmny.provider.model.vo.RpcUserVo;
import com.csun.cmny.provider.model.vo.merchant.MerchantVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacMerchantService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-06-06 16:55
 * @Description: the class uac user feign client
 */
@Slf4j
@RestController
@Api(value = "API - UacUserFeignClient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacUserFeignClient implements UacUserFeignApi {

    @Resource
    private UacUserService uacUserService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacMerchantService uacMerchantService;

    @Override
    public Wrapper<List<RpcUserVo>> queryUserListByUserIds(List<Long> userList) {
        return null;
    }

    @Override
    public Wrapper<List<OmcUserVo>> queryOmcUserListByUserIds(@RequestBody List<Long> userList) {
        List<OmcUserVo> list = uacUserService.queryOmcUserListByUserIds(userList);
        //return WrapMapper.ok(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), list);
    }

    @Override
    public Wrapper<List<OmcUserVo>> queryOmcUserListByQueryDto(@RequestBody OmcUserQueryDto omcUserQueryDto) {
        List<OmcUserVo> list = uacUserService.queryOmcUserListByQueryDto(omcUserQueryDto);
        //return WrapMapper.ok(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), list);
    }

    @Override
    public Wrapper<List<OmcUserVo>> queryOmcUnUnrsingUserListByUserIds(@RequestBody List<Long> userList, @RequestParam Long goupId, @RequestParam String roleCode) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("goupId",goupId);
        paramMap.put("roleCode",roleCode);
        paramMap.put("userList",userList);
        List<OmcUserVo> list = uacUserService.queryOmcUnUnrsingUserListByUserIds(paramMap);
        //return WrapMapper.ok(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), list);
    }

    @Override
    public Wrapper<List<RpcUserVo>> queryUserListByRoleCodeAndGroupId(@RequestBody UserRoleDto userRoleDto) {

        List<RpcUserVo> userVoList = uacUserService.queryUserListByRoleCodeAndGroupId(userRoleDto);

        //return WrapMapper.ok(userVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), userVoList);
    }

    @Override
    public Wrapper<OpcUserVo> queryOpcUserVoByUserId(@RequestParam("userId") Long userId) {

        OpcUserVo opcUserVo = uacUserService.queryOpcUserVoByUserId(userId);

        //return WrapMapper.ok(opcUserVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), opcUserVo);
    }

    @Override
    public Wrapper<List<RpcMerchantVO>> queryMerchantListByIdList(@RequestBody List<Long> userList) {


        List<RpcMerchantVO> rpcMerchantVOS = Lists.newArrayList();
        List<MerchantVo> merchantVos = uacMerchantService.listMerchantByIdList(userList);

        if(merchantVos.size() > 0){
            for (MerchantVo merchantVo : merchantVos) {
                RpcMerchantVO rpcMerchantVO = new RpcMerchantVO();
                rpcMerchantVO.setId(merchantVo.getId());
                rpcMerchantVO.setMerchantName(merchantVo.getMerchantName());
                rpcMerchantVO.setUserId(merchantVo.getUserId());
                rpcMerchantVOS.add(rpcMerchantVO);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), rpcMerchantVOS);
    }
}
